package com.timesheet.gui.efforttracker.controllers;

import com.timesheet.gui.efforttracker.composite.CreateNewEffortComposite;
import com.timesheet.gui.efforttracker.validators.NotNullValidator;
import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.common.models.view.EffortTrackerViewModel;
import com.timesheet.server.common.models.view.EffortTrackerViewModelContainer;
import com.timesheet.server.core.mapper.Mapper3;
import com.timesheet.serverice.EffortTrackerService;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.jface.databinding.fieldassist.ControlDecorationSupport;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.IViewerObservableValue;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class EffortTrackerCompositeController {

	CreateNewEffortComposite efforttrackerComposite = null;

	private EffortTrackerService effortTrackerService = new EffortTrackerService();

	private EffortTrackerViewModelContainer effortTrackerViewModelContainer = EffortTrackerViewModelContainer
			.getInstance();

	private EffortTrackerViewModel effortTrackerViewModel = new EffortTrackerViewModel();

	public EffortTrackerCompositeController(CreateNewEffortComposite composite) {

		efforttrackerComposite = composite;

		setValuesToCombo();

		createBinding();

		this.buttonClick();

	}

	public void buttonClick() {
		efforttrackerComposite.getBtnSave().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				try {

					EffortTrackerDTO effortTrackerDto = new EffortTrackerDTO();
					EffortTrackerDTO effortTrackerDto1 = Mapper3.map(effortTrackerDto, effortTrackerViewModel);
					effortTrackerService.fillEfforts(effortTrackerDto1);
					MessageDialog.openInformation(efforttrackerComposite.getShell(), "Info", "Successfully Saved");

				}
				catch (Exception exception) {
					exception.printStackTrace();
				}

			}
		});

	}

	private DataBindingContext createBinding() {

		DataBindingContext bindingContext = new DataBindingContext();

		// taskno
		{
			IObservableValue target = SWTObservables.observeText(efforttrackerComposite.getTxtTaskNo(), SWT.Modify);
			IObservableValue model =
					PojoProperties.value(EffortTrackerDTO.class, "taskNo").observe(effortTrackerViewModel);

			IValidator validator = new NotNullValidator("TaskNo should not be empty");
			UpdateValueStrategy strategy = new UpdateValueStrategy();
			strategy.setAfterGetValidator(validator);
			Binding bindValue = bindingContext.bindValue(target, model, strategy, null);
			ControlDecorationSupport.create(bindValue, SWT.TOP | SWT.LEFT);
		}

		// taskdescription
		{
			IObservableValue target =
					SWTObservables.observeText(efforttrackerComposite.getTxtDescription(), SWT.Modify);
			IObservableValue model =
					PojoProperties.value(EffortTrackerDTO.class, "taskDesciption").observe(effortTrackerViewModel);
			bindingContext.bindValue(target, model);
		}

		// taskphase
		{
			IViewerObservableValue observeSingleSelection =
					ViewersObservables.observeSingleSelection(efforttrackerComposite.getComboTaskPhaseViewer());
			IObservableValue observeValue = PojoObservables.observeValue(effortTrackerViewModel, "taskPhase");
			bindingContext.bindValue(observeSingleSelection, observeValue);
		}

		// TaskType
		{
			IViewerObservableValue observeSingleSelection =
					ViewersObservables.observeSingleSelection(efforttrackerComposite.getComboTaskTypeViewer());
			IObservableValue observeValue = PojoObservables.observeValue(effortTrackerViewModel, "taskType");
			bindingContext.bindValue(observeSingleSelection, observeValue);

		}

		// taskstatus

		{
			IViewerObservableValue observeSingleSelection =
					ViewersObservables.observeSingleSelection(efforttrackerComposite.getComboStatusViewer());
			IObservableValue observeValue = PojoObservables.observeValue(effortTrackerViewModel, "taskStatus");
			bindingContext.bindValue(observeSingleSelection, observeValue);
		}
		return bindingContext;
	}

	public void setValuesToCombo() {

		String[] taskphases = { "a", "b", "c" };

		String[] tasktypes = { "abc", "efg", "hij" };

		String[] taskstatuses = { "Completed", "Need help", "Just Started", "Not Yet Started" };

		ComboViewer comboViewerTaskPhase = efforttrackerComposite.getComboTaskPhaseViewer();
		comboViewerTaskPhase.setContentProvider(ArrayContentProvider.getInstance());
		comboViewerTaskPhase.setInput(taskphases);

		efforttrackerComposite.getComboTaskTypeViewer().setContentProvider(ArrayContentProvider.getInstance());

		efforttrackerComposite.getComboTaskTypeViewer().setInput(tasktypes);

		efforttrackerComposite.getComboStatusViewer().setContentProvider(ArrayContentProvider.getInstance());
		efforttrackerComposite.getComboStatusViewer().setInput(taskstatuses);

	}

}